#!/bin/bash

. ${SCRIPTS:-/}start-utils

export TYPE=FORGE

if [[ -z $FORGE_INSTALLER && -z $FORGE_INSTALLER_URL ]]; then
  norm=$VANILLA_VERSION

  case $VANILLA_VERSION in
    *.*.*)
      norm=$VANILLA_VERSION ;;
    *.*)
      norm=${VANILLA_VERSION}.0 ;;
  esac

  #################################################################################

  log "Checking Forge version information."
  case $FORGEVERSION in
    RECOMMENDED)
      curl -fsSL -o /tmp/forge.json http://files.minecraftforge.net/maven/net/minecraftforge/forge/promotions_slim.json
      FORGE_VERSION=$(cat /tmp/forge.json | jq -r ".promos[\"$VANILLA_VERSION-recommended\"]")
      if [ $FORGE_VERSION = null ]; then
        FORGE_VERSION=$(cat /tmp/forge.json | jq -r ".promos[\"$VANILLA_VERSION-latest\"]")
        if [ $FORGE_VERSION = null ]; then
          log "ERROR: Version $VANILLA_VERSION is not supported by Forge"
          log "       Refer to http://files.minecraftforge.net/ for supported versions"
          exit 2
        fi
      fi
      ;;

    *)
      FORGE_VERSION=$FORGEVERSION
      ;;
  esac

  normForgeVersion=$VANILLA_VERSION-$FORGE_VERSION-$norm
  shortForgeVersion=$VANILLA_VERSION-$FORGE_VERSION

  FORGE_INSTALLER="/tmp/forge-$shortForgeVersion-installer.jar"
elif [[ -z $FORGE_INSTALLER ]]; then
  FORGE_INSTALLER="/tmp/forge-installer.jar"
elif [[ ! -e $FORGE_INSTALLER ]]; then
  log "ERROR: the given Forge installer doesn't exist : $FORGE_INSTALLER"
  exit 2
fi

installMarker="/data/.forge-installed-$shortForgeVersion"

if [ ! -e $installMarker ]; then
  if [ ! -e $FORGE_INSTALLER ]; then

    if [[ -z $FORGE_INSTALLER_URL ]]; then
      log "Downloading $normForgeVersion"

      forgeFileNames="
      $normForgeVersion/forge-$normForgeVersion-installer.jar
      $shortForgeVersion/forge-$shortForgeVersion-installer.jar
      END
    "
      for fn in $forgeFileNames; do
        if [ $fn == END ]; then
          log "Unable to compute URL for $normForgeVersion"
          exit 2
        fi
        downloadUrl=http://files.minecraftforge.net/maven/net/minecraftforge/forge/$fn
        log "...trying $downloadUrl"
        if curl -o $FORGE_INSTALLER -fsSL $downloadUrl; then
          break
        fi
      done
    else
      log "Downloading $FORGE_INSTALLER_URL ..."
      if ! curl -o $FORGE_INSTALLER -fsSL $FORGE_INSTALLER_URL; then
        log "Failed to download from given location $FORGE_INSTALLER_URL"
        exit 2
      fi
    fi
  fi

  log "Installing Forge $shortForgeVersion using $FORGE_INSTALLER"
  mkdir -p mods
  tries=3
  while ((--tries >= 0)); do
    java -jar $FORGE_INSTALLER --installServer
    if [ $? == 0 ]; then
      break
    fi
  done
  if (($tries < 0)); then
    log "Forge failed to install after several tries." >&2
    exit 10
  fi
  # NOTE $shortForgeVersion will be empty if installer location was given to us
  log "Finding installed server jar..."
  unset -v latest
  for file in *forge*.jar; do
    [[ $file =~ installer ]] || [[ $file -nt $latest ]] && latest=$file
  done
  if [[ -z $latest ]]; then
    log "Unable to derive server jar for Forge"
    exit 2
  fi

  export SERVER=$latest
  log "Using server $SERVER"
  echo $SERVER > $installMarker

else
  export SERVER=$(cat $installMarker)
fi

# Continue to Final Setup
exec ${SCRIPTS:-/}start-finalSetupWorld $@
